home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / djgpp / src / libgplus.5 / libgplus / etc / lf / entry.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-25  |  2.6 KB  |  105 lines

  1. /* Manipulate directory entries of a particular file class. */
  2. #include <std.h>
  3. #include "entry.h"
  4.  
  5. /* Initially provide a medium-sized file entry size. */
  6. const int Entry_Handler::default_entries = 50;
  7.  
  8. /* Print entries to standard output.  The algorithm used to format the files
  9.    in columns is subtle, and worth studying in some detail. */
  10.  
  11. void
  12. Entry_Handler::print_entries (char *class_name) 
  13. {
  14.   const int  width = Screen_Handler::screen_width ();
  15.   const int  ncols = width / (max_entry_length + 1);
  16.   const int  nrows = (entries + ncols - 1) / ncols;
  17.   const int  max   = nrows * (entries - (nrows - 1) * ncols);
  18. #ifdef __GNUC__
  19.   char buffer[width];
  20. #else
  21.   char *buffer = new char[width];
  22. #endif
  23.  
  24.   /* Print out the file class name, nicely centered and in inverse video. */
  25.   sprintf (buffer, "[ %d %s File%s ]", entries, class_name, entries == 1 ? "" : "s");
  26.   Screen_Handler::print_inverse_centered (buffer);
  27.  
  28. #ifndef __GNUC__
  29.   delete [] buffer;
  30. #endif
  31.  
  32.   /* Take care of everything but the (possibly non-existent) final row. */
  33.   
  34.   for (int row = 0; row < nrows - 1; row++)
  35.     {
  36.       /* This loop is subtle, since we don't want to process too many entries.... */
  37.       
  38.       for (int col = row; col < entries; col += col < max ? nrows : nrows - 1)
  39.         printf ("%-*s", max_entry_length + 1, buf[col]);
  40.       
  41.       putchar ('\n');
  42.     }
  43.   /* Treat the final row specially, if it exists. */
  44.   
  45.   for (; row < max; row += nrows) 
  46.     printf ("%-*s", max_entry_length + 1, buf[row]);
  47.   
  48.   putchar ('\n');
  49. }
  50.  
  51. #ifdef __GNUC__ /* Already defined in new.h ? */
  52. /* Your basic realloc, in C++.  */
  53. static void *operator new (size_t, void *orig, size_t newsize)
  54. {
  55.   void *p = (void *)realloc (orig, newsize);
  56.   if (p == 0)
  57.     (*__new_handler) ();
  58.   return p;
  59. }
  60. #endif
  61.  
  62. /* Only compile these functions if -O is *not* enabled. */
  63.  
  64. #ifndef __OPTIMIZE__
  65.  
  66. /* Initialize a new file class. */
  67.  
  68. Entry_Handler::Entry_Handler (void)
  69. {
  70.   entries = max_entry_length = 0;
  71.   total_entries = default_entries;
  72.   buf = new char *[default_entries];
  73. }
  74.  
  75. /* Current number of file entries. */
  76.  
  77. int 
  78. Entry_Handler::entry_number (void)
  79. {
  80.   return entries;
  81. }
  82.  
  83. /* Add an entry to the file class. */
  84.  
  85. void 
  86. Entry_Handler::add_entry (char *entry_name, int length)
  87. {
  88.   /* Grow the buffer on overflow. */
  89.   if (entries >= total_entries)
  90.     buf = new (buf, total_entries *= 2) char *;
  91.   if (max_entry_length < length)
  92.     max_entry_length = length;
  93.   buf[entries++] = strcpy (new char[length + 1], entry_name);
  94. }
  95.  
  96. /* Sort entries by filename. */
  97.  
  98. void 
  99. Entry_Handler::sort_entries (void)
  100. {
  101.   sort (buf, entries);
  102. }
  103.  
  104. #endif                          // __OPTIMIZE__
  105.